
#ifndef _I7816_UTILS_H_
#define _I7816_UTILS_H_

#include <stdint.h>
#include <stdbool.h>

#define I7816_BIT_MASK(bnum)            ((uint32_t)0x01 << (bnum))
#define I7816_BIT_GET(val, bnum)        ((uint32_t)((val) >> (bnum)) & (uint32_t)0x01)
#define I7816_BIT_SET(val, bnum)        do{ (val) |= I7816_BIT_MASK(bnum); }while(0)
#define I7816_BIT_CLR(val, bnum)        do{ (val) &= ~ I7816_BIT_MASK(bnum); }while(0)
#define I7816_BIT_TEST(val, bnum)       (((val) & I7816_BIT_MASK(bnum)) != 0)

static inline int i7816_max(int a, int b){
    return a > b ? a : b;
}

static inline int i7816_min(int a, int b){
    return a < b ? a : b;
}

uint16_t i7816_convert_fi_code_to_value(uint8_t fi_code);

uint8_t i7816_convert_di_code_to_value(uint8_t di_code);

uint8_t i7816_reverse_byte(uint8_t ch);

uint16_t i7816_to_inverse(uint16_t data);

uint32_t i7816_etu2clk(uint32_t etu, uint8_t FiDi);

uint8_t i7816_nibble_setbit_number(uint8_t data);

uint8_t i7816_xor_sequence(uint8_t buf[], uint32_t len);

#endif	//_I7816_HAL_H_

